package com.ymcv.proyecto7.tools;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

public class Confirm extends AsyncTask<String, Void, String> {

	Context context;
	boolean acept;

	public Confirm(Context context) {
		this.context = context;
	}

	@Override
	protected String doInBackground(String... valores) {
		String msj = "";

		try {
			//String url = "http://199.192.79.50/proyecto7/ws/";
			String url ="http://desarrollo.yoelcardenas.com/";
			if (valores[2].equalsIgnoreCase("si")) {
				acept = true;
			} else {
				acept = false;
			}
			HttpGet get = new HttpGet(url + "cabconfirm.php?cabid="
					+ valores[0] + "&tranid=" + valores[1] + "&acept="
					+ valores[2]);

			HttpClient cliente = new DefaultHttpClient();

			HttpResponse response = cliente.execute(get);
			InputStream is = response.getEntity().getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is,
					"UTF-8"), 8);
			StringBuilder sb = new StringBuilder();
			String linea = null;
			while ((linea = br.readLine()) != null) {
				sb.append(linea);
			}
			msj = sb.toString();
			is.close();
			br.close();
		} catch (Exception e) {
			acept = false;
		}
		return msj;
	}

	@Override
	protected void onPostExecute(String result) {
		Intent intent = new Intent("reserva");
		Bundle b = new Bundle();
		try {
			JSONObject json = new JSONObject(result);
			b.putString("pago", json.getString("pago"));
			b.putString("tarifa", json.getString("tarifa"));
			b.putBoolean("acept", acept);
		} catch (Exception e) {
			b.putBoolean("acept", false);
		}
		intent.putExtras(b);
		context.sendBroadcast(intent);
	}

}
